package net.chathu.airgw
{
	import com.bit101.components.ScrollBar;
	import net.chathu.airgw.ui.TextArea;
	
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.net.Socket;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import net.chathu.airgw.model.conn.IRCConnection;
	import net.chathu.airgw.model.conn.IRCConnectionListener;
	import net.chathu.airgw.model.UserList;
	import net.chathu.airgw.packets.gw.clientpackets.CMDWhois;
	import net.chathu.airgw.packets.gw.clientpackets.IRCReceivablePacket;
	import net.chathu.airgw.ui.UITest;
	import net.chathu.airgw.util.IDebugProxy;

	/**
	 * ...
	 * @author ze
	 */
	public class Main extends Sprite implements IDebugProxy
	{
		private var logBox:TextArea;
		private var listener:IRCConnectionListener;
		private static var _instance:Main;
		[Embed (source="/../assets/images/airgw_logo_big.png")]
		private var logoClass:Class;
		private var logo:*;
		
		public function Main():void 
		{
			
			_instance = this;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			stage.nativeWindow.addEventListener(Event.CLOSING, onClosing);
			
			logo = new logoClass();
			addChild(logo);
			logo.x = stage.stageWidth / 2 - logo.width / 2;
			logBox = new TextArea(this);
			logBox.autoScroll = true;
			stage.addEventListener(Event.RESIZE, onResize);
			logBox.width = stage.stageWidth;
			logBox.height = stage.stageHeight - 52;
			logBox.y = 52;
			logBox.editable = false;
			Debug.proxy = this;
			Debug.echo("Start");
			Config.reload();
			stage.nativeWindow.title = Config.PROGRAM_NAME + " v" + Config.PROGRAM_VERSION + (Config.IS_DEBUG_BUILD ? " [DEBUG VERSION]":"");
			
			AutoUpdater.check(initApp);	
		}
		
		private function initApp(e:*):void {
			new UITest(stage.nativeWindow);
			
			startListening();
			
			//Debug.echo("Ready");
			//Debug.echo("Waiting for incoming connections on port: " + listener.port); 
			
			// IRC kliens nelkuli teszteles: //
			/*var s:Soul = SoulManager.createSoul();
			s.accountData.username = "...";
			s.accountData.password = "...";
			s.webLogin();*/	

		}
		
		private function onResize(event:Event):void {
			logBox.width = stage.stageWidth;
			logBox.height = stage.stageHeight - 52;
			logo.x = stage.stageWidth / 2 - logo.width / 2;
		}
		
		private function onClosing(event:Event):void {
			if (Config.HIDE_APPLICATION_ON_CLOSE_CLICK===true)
			{
				var nw:* = stage.nativeWindow;
				stage.nativeWindow.visible = false;
				event.stopImmediatePropagation();
				event.stopPropagation();
				event.preventDefault();
			}
		}

		
		private function startListening():void {
			try {
				listener = new IRCConnectionListener(Config.LISTENING_PORT);
				listener.start();
			} catch (err:Error ) {
				Debug.echo("(Notice: Every time you bind already used port, God kills a puppy!)");
			}
			
		}
		
		public function onEcho(msg:String):void {
			if (logBox) {
				logBox.text = logBox.text + msg + "\n";
			}
		}
	
		
		public static function onApplicationUpdate():void {
			if (_instance.listener)
			{
				_instance.listener.destroy();
				_instance.listener = null;
			}
			
			SoulManager.destroy();
		}
		
		
	}
	
}